/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.excelListener
 * 创建时间：2020年03月16日 14时15分
 **/
package com.zhongwang.mms.module.purchase.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.mms.gen.entity.POrderDetail;
import com.zhongwang.mms.module.purchase.dao.PurPOrderDetailDao;
import com.zhongwang.mms.module.purchase.dao.PurWarInManageDao;
import com.zhongwang.mms.module.purchase.model.PInModel;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName： PurWarInManageListener
 * @Description：
 * @Author： 徐一贺
 * @Date： 2020/3/16 14:15
 * @Version： V1.0
 **/
public class PurWarInManageListener extends AnalysisEventListener<PInModel> {

    //外购件入库持久层
    private PurPOrderDetailDao purPOrderDetailDao;
    private PurWarInManageDao purWarInManageDao;
    private PInModel pInModel;
    private String uploadPath;
    /**
     * 由于sqlserver 2100 参数限制， 每隔80条存储数据库，
     */
    private static final int BATCH_COUNT = 80;

    private List<PInModel> dataList = new ArrayList<PInModel>();       // 正常数据
    /**
     * @Description: 异常信息接口
     * @Author: 徐一贺
     * @CreateDate: 2020/3/17 13:42
     */
    private ErrorDataInterface errorDataInterface;
    private List<PInModel> errorData = new ArrayList<>();     // 异常数据
    private int pOdrId;


    public PurWarInManageListener(PurWarInManageDao purWarInManageDao, PurPOrderDetailDao purPOrderDetailDao, int pOdrId , ErrorDataInterface errorDataInterface) {
        this.purWarInManageDao = purWarInManageDao;
        this.purPOrderDetailDao = purPOrderDetailDao;
        this.errorDataInterface = errorDataInterface;
        this.pOdrId = pOdrId;
    }


    @Override
    public void invoke(PInModel pInModel, AnalysisContext analysisContext) {
        dataList.add(pInModel);
        if (dataList.size() >= BATCH_COUNT) {
            //循环插入从表数据与库存数据
            for (int i = 0; i < dataList.size(); i++) {
                //循环插入从表
                POrderDetail pOrderDetail = new POrderDetail();
                pOrderDetail.setOdaMatCode(dataList.get(i).getMatCode());
                pOrderDetail.setOdaMatDesc(dataList.get(i).getMatDesc());
                pOrderDetail.setOdaOdrId(pOdrId);
                pOrderDetail.setOdaType(1);
                pOrderDetail.setOdaNum(dataList.get(i).getNum());
                pOrderDetail.setOdaNumUnit(dataList.get(i).getUnit());
                pOrderDetail.setOdaTotalPrice(dataList.get(i).getAmount());
                pOrderDetail.setOdaNumPrice(dataList.get(i).getPrice());
                pOrderDetail.setOdaTrackCode(dataList.get(i).getPlanTrackingCode());
                pOrderDetail.setOdaProName(dataList.get(i).getProName());
                pOrderDetail.setOdaProCode(dataList.get(i).getProCode());
                pOrderDetail.setOdaWhiId(purWarInManageDao.findWhiIdByWhiCode(dataList.get(i).getOdaWhi()));
                pOrderDetail.setOdaPosition(dataList.get(i).getOdaPosition());
                try {
                    pOrderDetail.setOdaBookkeepingDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dataList.get(i).getOdaBackDate()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                pOrderDetail.setOdaSpec(dataList.get(i).getSpecialInstructions());
                purPOrderDetailDao.insert(pOrderDetail);
                // 循环插入库存表
            }
            dataList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (dataList.size() >0) {
            //循环插入从表数据与库存数据
            for (int i = 0; i < dataList.size(); i++) {
                //循环插入从表
                POrderDetail pOrderDetail = new POrderDetail();
                pOrderDetail.setOdaMatCode(dataList.get(i).getMatCode());
                pOrderDetail.setOdaMatDesc(dataList.get(i).getMatDesc());
                pOrderDetail.setOdaOdrId(pOdrId);
                pOrderDetail.setOdaType(1);
                pOrderDetail.setOdaNum(dataList.get(i).getNum());
                pOrderDetail.setOdaNumUnit(dataList.get(i).getUnit());
                pOrderDetail.setOdaTotalPrice(dataList.get(i).getAmount());
                pOrderDetail.setOdaNumPrice(dataList.get(i).getPrice());
                pOrderDetail.setOdaTrackCode(dataList.get(i).getPlanTrackingCode());
                pOrderDetail.setOdaProName(dataList.get(i).getProName());
                pOrderDetail.setOdaProCode(dataList.get(i).getProCode());
                pOrderDetail.setOdaWhiId(purWarInManageDao.findWhiIdByWhiCode(dataList.get(i).getOdaWhi()));
                pOrderDetail.setOdaPosition(dataList.get(i).getOdaPosition());
                try {
                    pOrderDetail.setOdaBookkeepingDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dataList.get(i).getOdaBackDate()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                pOrderDetail.setOdaSpec(dataList.get(i).getSpecialInstructions());
                purPOrderDetailDao.insert(pOrderDetail);
                // 循环插入库存表
            }
        }
        errorDataInterface.handler(errorData);
    }

    /**
     * 异常数据捕获
     *
     * @param exception
     * @param context
     * @throws Exception
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        pInModel = new PInModel();
//        Map<Integer, CellData> map = (Map<Integer, CellData>) context.readRowHolder().getCurrentRowAnalysisResult();
        errorData.add(pInModel);
    }
}